package com.example.bgxqwebsocket.controller;


import com.example.bgxqapi.config.qiniu.QiNiuConfig;
import com.example.bgxqapi.domain.ApiResult;
import com.example.bgxqapi.domain.factory.MessageFactory;
import com.example.bgxqapi.domain.receive.ReceiveMessage;
import com.example.bgxqapi.domain.result.SendMessageResult;
import com.example.bgxqapi.domain.result.UnreadMessageResult;
import com.example.bgxqapi.domain.send.SendMessage;
import com.example.bgxqapi.domain.send.SendMessageType;
import com.example.bgxqapi.domain.send.UserMessage;
import com.example.bgxqapi.exception.MissingParamException;
import com.example.bgxqapi.exception.NoPermissionException;
import com.example.bgxqapi.exception.ResourseNotExistException;
import com.example.bgxqapi.util.UserUtil;
import com.example.bgxqwebsocket.service.MessageService;
import com.example.bgxqwebsocket.service.WebSocket;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import javax.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

/**
 * @author lizijian
 */
@RequestMapping("/bgxq/message")
@RestController
@Validated
@Api(tags = "消息模块")
@Slf4j
public class MessageController {

  @Autowired
  private MessageService messageService;


  @ApiOperation(value = "向指定id用户发送消息",notes = "返回消息的id")
  @PostMapping("/send")
  public ApiResult<SendMessageResult> sendMessage(@Validated ReceiveMessage receiveMessage)
      throws MissingParamException, NoPermissionException {
    int messageId = messageService.sendMessage(
        MessageFactory.getMessage(receiveMessage.getContent(), UserUtil.getUserId(),
            receiveMessage.getToUserId(), receiveMessage.getType(), receiveMessage.getTime()));
      boolean success = WebSocket.sendMessage(receiveMessage.getToUserId(),
          new SendMessage<>(SendMessageType.USER_MESSAGE.getName(),
              new UserMessage(messageId, receiveMessage.getContent(),
                  receiveMessage.getType(), UserUtil.getUserId(), receiveMessage.getTime())));
      //如果未成功发送消息,消息存储至未读消息表
      if(!success){
        messageService.saveUnreadMessage(messageId);
      }
    return ApiResult.success(new SendMessageResult(messageId, QiNiuConfig.URL));
  }

  @PostMapping("/{id}/withdraw")
  @ApiOperation("撤回指定id的消息")
  public ApiResult<?> withdraw(@NotNull @PathVariable Integer id)
      throws ResourseNotExistException, NoPermissionException {
    messageService.withdraw(id);
    return ApiResult.success();
  }

  @ApiOperation("获取指定id用户发送的未读消息")
  @DeleteMapping("/unRead/message")
  public ApiResult<List<UnreadMessageResult>> getUnreadMessage(@NotNull Integer userId){
    return ApiResult.success(messageService.getUnreadMessage(userId));
  }

  @ApiIgnore
  @PostMapping("/interactive/send")
  public ApiResult<Boolean> sendInteractiveMessage(@RequestParam int toUserId,@RequestBody SendMessage<?> sendMessage){
   return ApiResult.success(WebSocket.sendMessage(toUserId, sendMessage));
  }

}
